//
//  Utils.m
//  SourceListTest
//
//  Created by John on 09-3-18.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Utils.h"


@implementation Utils

+ (void)initAMTDir
{
	NSString *amtDir = [[NSHomeDirectory()
						 stringByAppendingPathComponent:@"Music"]
						stringByAppendingPathComponent:@"AMT"];
	NSString *musicDir = [amtDir stringByAppendingPathComponent:@"Music"];
	NSString *movieDir = [amtDir stringByAppendingPathComponent:@"Movie"];
	NSString *pictureDir = [amtDir stringByAppendingPathComponent:@"Picture"];
	
	NSFileManager *fileManager = [NSFileManager defaultManager];
	
	if (![fileManager fileExistsAtPath:amtDir]) {
		[fileManager createDirectoryAtPath:amtDir attributes:nil];
	}
	
	if (![fileManager fileExistsAtPath:musicDir]) {
		[fileManager createDirectoryAtPath:musicDir attributes:nil];
	}
	
	if (![fileManager fileExistsAtPath:movieDir]) {
		[fileManager createDirectoryAtPath:movieDir attributes:nil];
	}
	
	if (![fileManager fileExistsAtPath:pictureDir]) {
		[fileManager createDirectoryAtPath:pictureDir attributes:nil];
	}
}
///这里测试用的，以后可以识别设备后，查看设备的根目录下有没有那三个目录。若没有就创建
+ (void)initDeviceDir
{
	NSString *deviceDir = [[NSHomeDirectory()
						 stringByAppendingPathComponent:@"Music"]
						stringByAppendingPathComponent:@"Device"];
	NSString *musicDir = [deviceDir stringByAppendingPathComponent:@"Music"];
	NSString *movieDir = [deviceDir stringByAppendingPathComponent:@"Movie"];
	NSString *pictureDir = [deviceDir stringByAppendingPathComponent:@"Picture"];
	
	NSFileManager *fileManager = [NSFileManager defaultManager];
	
	if (![fileManager fileExistsAtPath:deviceDir]) {
		[fileManager createDirectoryAtPath:deviceDir attributes:nil];
	}
	
	if (![fileManager fileExistsAtPath:musicDir]) {
		[fileManager createDirectoryAtPath:musicDir attributes:nil];
	}
	
	if (![fileManager fileExistsAtPath:movieDir]) {
		[fileManager createDirectoryAtPath:movieDir attributes:nil];
	}
	
	if (![fileManager fileExistsAtPath:pictureDir]) {
		[fileManager createDirectoryAtPath:pictureDir attributes:nil];
	}
}


+ (BOOL)copyFileWithName:(NSString *)filename AndType:(int)type AndToPath:(NSString *)toPath
{
	
	//	NSString *docsDir = filename;
	if(NSFileTypeDirectory == [[[NSFileManager defaultManager] fileAttributesAtPath:filename traverseLink:NO] objectForKey:NSFileType]) {
		NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:filename];
		
		NSString *file;
		while (file = [dirEnum nextObject]) {
			NSString *realFilePath = [filename stringByAppendingPathComponent:file];
			NSString* filetype = [[[NSFileManager defaultManager] fileAttributesAtPath:realFilePath traverseLink:NO] objectForKey:NSFileType];
			if (filetype!=NSFileTypeDirectory) {
				//操作
//				NSLog(realFilePath);
				if(![Utils copyOneFileWithName:realFilePath AndToPath:toPath]) {
					return NO;
				}
			}
		}
	} else if(![Utils copyOneFileWithName:filename AndToPath:toPath]) {
		return NO;
	}
	return YES;
}



+ (BOOL)copyOneFileWithName:(NSString *)tmp_filename AndToPath:(NSString *)toPath
{
	NSError *error = nil;
	//遍历子目录，过滤，拷贝当前Table所支持的格式文件。(弹出对话框，提示，当前的进度，转换中，或者拷贝中，什么的。提供暂停和停止按钮)
	//为了能使前台界面可以继续操作，这里需要启动线程，完成转换和拷贝工作，前台可以在对话框中控制进程的运行或者暂停和停止。
	NSString *toPathName = [toPath stringByAppendingPathComponent:[tmp_filename lastPathComponent]];
	
	if([[NSFileManager defaultManager] fileExistsAtPath:toPathName])
	{
		int index = 0;
		do{
			index++;
			NSString *addPartStr = [NSString stringWithFormat:@"(%d).",index];
			NSString *lastPathComp = [tmp_filename lastPathComponent];
			NSUInteger location= [lastPathComp rangeOfString:[tmp_filename pathExtension]].location;
			lastPathComp = [[[lastPathComp substringToIndex:location-1]stringByAppendingString:addPartStr]stringByAppendingString:[lastPathComp pathExtension]];
			NSLog(lastPathComp);
			toPathName = [toPath stringByAppendingPathComponent:lastPathComp];
		}
		while([[NSFileManager defaultManager] fileExistsAtPath:toPathName]);
	}
	
	
	BOOL copyResult = [[NSFileManager defaultManager] copyItemAtPath:tmp_filename
															  toPath:toPathName
															   error:&error];
	
	if(!copyResult)
	{
		//拷贝出错，发出警告，停止剩余操作
		NSLog(@"Copy Error!!! : %@",error);
		return NO;
	}
	return YES;
}


+ (int)fileTypeFilter:(NSString *)tmp_filename type:(int)tmp_type
{
	NSArray *types = nil;
	NSString *allowCopyType = nil;
	if(tmp_type == fileType_movie) {
		types = [NSArray arrayWithObjects: @"MOV", @"RM", @"RMVB", @"AVI", @"MP4", @"3GP", @"FLV", @"MKV",nil];
		allowCopyType = @"MMV";
	} else if(tmp_type == fileType_music) {
		types = [NSArray arrayWithObjects: @"AAC", @"WMA", @"WAV", @"OGG", @"MP1", @"MP2", @"APE", @"FLAC",nil];
		allowCopyType = @"MP3";
	} else if(tmp_type == fileType_picture) {
		types = [NSArray arrayWithObjects: @"JPEG",@"JPG", @"BMP",nil];
	}
	
	//	if(types == nil)
	//		return NO;
	for(NSString *type in types)
	{	//是否大小写，不需要大小写
		if(tmp_type == fileType_picture)
		{
			if ([[[tmp_filename pathExtension] uppercaseString]isEqualToString: type]) {
				return dragDeal_copy;
			}
		} else if ([[[tmp_filename pathExtension] uppercaseString]isEqualToString: type])  {
			return dragDeal_convert;
		} else {
			if([[[tmp_filename pathExtension] uppercaseString]isEqualToString: allowCopyType]) {
				return dragDeal_copy;
			}
		}
	}
	return dragDeal_deny;
}
@end
